/*
 * Class ObjectOption1
 *
 * Version 1.0
 *
 * Friday, August 22, 2008
 *
 * Created by Palidino76
 */

package palidino76.rs2.io.packets;

import palidino76.rs2.Engine;
import palidino76.rs2.players.Player;
import palidino76.rs2.util.Misc;

public class ObjectOption1 implements Packet {
    /**
     * make sure to document EVERY coordinate to go with each object unless an un-important object(wilderness ditch lol).
     * This will prevent people from spawning an object client side and actually using it.
     * So make sure to include with the id, objectX == # && objectY == #
     */

    /**
     * Handles the first option on objects.
     * @param p The Player which the frame should be handled for.
     * @param packetId The packet id this belongs to.
     * @param packetSize The amount of bytes being recieved for this packet.
     */
    public void handlePacket(Player p, int packetId, int packetSize) {
        if (p == null || p.stream == null) {
            return;
        }
        if (!p.objectOption1) {
            p.clickX = p.stream.readUnsignedWordBigEndian();
            p.clickId = p.stream.readUnsignedWord();
            p.clickY = p.stream.readUnsignedWordBigEndianA();
            if (Misc.getDistance(p.absX, p.absY, p.clickX, p.clickY) > 30) {
                return;
            }
            p.objectOption1 = true;
        }
        int distance = Misc.getDistance(p.clickX, p.clickY, p.absX, p.absY);
        if (p.walkDir != -1 || p.runDir != -1 || distance > objectSize(p.clickId)) {
            return;
        }
        p.objectOption1 = false;
        switch (p.clickId) {
	case 23271:
	  	p.jumpDelay = 3;
	  if (p.absY > p.clickY) {
		p.reqWalkQueue(p.absX, p.clickY - 1);
	  }
	  if (p.absY < p.clickY) {
		p.reqWalkQueue(p.absX, p.clickY + 2);
	  }
	  break;
        default:
            Misc.println("[" + p.username + "] Unhandled object 1: " + p.clickId);
            break;
        }
    }

    private int objectSize(int id) {
        switch (id) {
        case 23271:
            return 2;
        default:
            return 1;
        }
    }
}
